package com.rui.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class DemoSpinlockTest {
    public static void main(String[] args) throws InterruptedException {
//        ReentrantLock reentrantLock = new ReentrantLock();
//        reentrantLock.lock();
//        reentrantLock.unlock();

        //底层使用的自旋锁
        DemoSpinlock demoSpinlock = new DemoSpinlock();

        new Thread(()->{
            demoSpinlock.mylock();
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                demoSpinlock.mylock();
            }
        },"t1").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(()->{
            demoSpinlock.mylock();
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                demoSpinlock.mylock();
            }
        },"t2").start();
    }
}
